import { Byte } from '../../api/io/Byte';
import { ITimer } from '../../api/timer/ITimer';
import { IAsync } from '../../api/event/IAsync';



/**战斗结果属性枚举*/
export const enum E_BattleResult {
	/**胜利方*/
	WIN_CAMP = 0,
	/**战斗耗时*/
	USE_TIME = 1,
	/**枚举数量*/
	EnumCount = 2
}


/**战斗结果*/
export class BattleResult {
	/**事件ID*/
	public static readonly EVENT_ID = 'STRUCT_DATA_BATTLE_RESULT_EVENT_ID';
	/**整体更新事件ID（部分只需要监听整体变化）*/
	public static readonly TOTAL_EVENT_ID = 'STRUCT_DATA_BATTLE_RESULT_TOTAL_EVENT_ID';
	/**缓存池*/
	private static POOL: BattleResult[] = [];

	/**创建入口*/
	public static create(json?: string): BattleResult {
		const c = this.POOL.pop() || new BattleResult();
		c._destroyed = false;
		c.resetProperty();
		if (json) {
			c.parse(json);
		}
		return c;
	}


	/**已释放*/
	protected _destroyed = false;
	/**胜利方 (uint8)*/
	protected _winCamp: number;
	/**战斗耗时 (uint32)*/
	protected _useTime: number;


	/**构建函数-私有，防止外部创建*/
	private constructor() {
	}


	/**释放*/
	public destroy(): void {
		if (this._destroyed) {
			return;
		}
		this._destroyed = true;
		this.resetProperty(true);
		if (BattleResult.POOL.indexOf(this) === -1) {
			BattleResult.POOL.push(this);
		}
	}

	/**胜利方*/
	public get winCamp() {
		return this._winCamp;
	}


	/**胜利方*/
	public set winCamp(v: number) {
		this._winCamp = v;
	}


	/**战斗耗时*/
	public get useTime() {
		return this._useTime;
	}


	/**战斗耗时*/
	public set useTime(v: number) {
		this._useTime = v;
	}



	/**通过枚举获取值*/
	public getV(k: E_BattleResult): any {
		switch (k) {
			case E_BattleResult.WIN_CAMP: return this._winCamp;
			case E_BattleResult.USE_TIME: return this._useTime;
			default: return null;
		}
	}


	/**通过枚举设置值*/
	public setV(k: E_BattleResult, v: any): void {
		switch (k) {
			case E_BattleResult.WIN_CAMP: this._winCamp = v; break;
			case E_BattleResult.USE_TIME: this._useTime = v; break;
			default: break;
		}
	}


	/**初始化*/
	public resetProperty(fromDestroy = false): void {
		//胜利方
		this._winCamp = 0;
		//战斗耗时
		this._useTime = 0;
	}


	/**序列化*/
	public stringify(toString: boolean): string | any {
		const json: any = {};
		if (this._winCamp) {
			json.winCamp = this._winCamp;
		}
		if (this._useTime) {
			json.useTime = this._useTime;
		}

		return toString ? JSON.stringify(json) : json;
	}


	/**反序列化*/
	public parse(jsonv: string | any): this {
		this.resetProperty();
		if (!jsonv) {
			return this;
		}
		const json = typeof (jsonv) === 'string' ? JSON.parse(jsonv) : jsonv;
		if (json.winCamp) {
			this._winCamp = json.winCamp;
		}
		if (json.useTime) {
			this._useTime = json.useTime;
		}
		return this;
	}


	/**序列化-二进制*/
	public toBytes(byte: Byte): void {
		if (!byte) {
			return;
		}
		byte.writeUint8(this._winCamp);
		byte.writeUint32(this._useTime);
	}


	/**反序列化-二进制*/
	public fromBytes(byte: Byte): this {
		this.resetProperty();
		if (!byte) {
			return;
		}
		this._winCamp = byte.readUint8();
		this._useTime = byte.readUint32();
		return this;
	}


	/**克隆*/
	public clone(): BattleResult {
		const byte = Byte.createFromPool();
		this.toBytes(byte);
		byte.pos = 0;
		const clonev = BattleResult.create().fromBytes(byte);
		Byte.recoverToPool(byte);
		return clonev;
	}


	/**处理更新*/
	public processUpdate(jsonv: string): void {
		if (!jsonv) {
			return;
		}

		const eventID = BattleResult.EVENT_ID;
		const us = JSON.parse(jsonv);

		for (let i = 0, len = us.length; i < len;) {
			const idx = us[i];
			//胜利方
			if (idx === E_BattleResult.WIN_CAMP) {
				const oldv = this._winCamp;
				this._winCamp = us[i + 1];
				IAsync.dispatch(eventID, idx, this._winCamp, oldv);
				i += 2;
				continue;
			}
			//战斗耗时
			if (idx === E_BattleResult.USE_TIME) {
				const oldv = this._useTime;
				this._useTime = us[i + 1];
				IAsync.dispatch(eventID, idx, this._useTime, oldv);
				i += 2;
				continue;
			}

			console.error('[Struct Data] can not find enum in BattleResult with idx = ' + idx + ', version maybe wrong.')
		}

		//全更事件
		ITimer.callLater(this, this.dispatchTotalUpdate);
	}


	/**比对不一致结果*/
	public getCompareInfo(other: BattleResult): string {
		if (!other) {
			return null;
		}

		const updates: any[] = [];

		if (this._winCamp !== other._winCamp) {
			updates.push(E_BattleResult.WIN_CAMP, this._winCamp);
		}
		if (this._useTime !== other._useTime) {
			updates.push(E_BattleResult.USE_TIME, this._useTime);
		}

		const jsonv = updates.length === 0 ? null : JSON.stringify(updates);
		return jsonv;
	}


	/**全部更新*/
	protected dispatchTotalUpdate(): void {
		IAsync.dispatch(BattleResult.TOTAL_EVENT_ID, this.getV(0));
	}

}
